﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
using WebSysCore.JSons;
using WebSysCore;
using WebSysCore.Helpers;
using HSchSchedule.Utils;


namespace HSchSchedule.AjaxPages
{
    public partial class AjaxAddSubject : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [WebMethod(true)]
        public static string AddSubject(List<JSubject> lstJSubject)
        {
            int cusId;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                bool ok = true;
                string err = "";
                foreach (JSubject js in lstJSubject)
                {
                    if (!ValidateHelp.ValidateSubject(js, out err))
                    {
                        ok = false;
                        break;
                    }

                }
                if (ok)
                {
                    int schId;
                    int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                    if (schId > 0)
                    {
                        List<Subject> lstSubject = new List<Subject>();
                        for (int i = 0; i < lstJSubject.Count; i++)
                        {
                            Subject d = AssignHelp.JSubToSub(lstJSubject[i]);
                            d.ScheduleInListId = schId;
                            lstSubject.Add(d);
                        }
                        DataBase.InsertSubject(lstSubject);
                        return GUIConstants.SUCCESS_RESULT;
                    }
                    else
                    {
                        return GUIConstants.FAIL_RESULT;
                    }
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }

        }
    }
}
